package com.xuanwu.shop.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xuanwu.shop.dao.CategoryDaoInterface;
import com.xuanwu.shop.entity.Category;

public class CategoryDaoImpl extends BaseDao implements CategoryDaoInterface {

	@Override
	public List<Category> findFirstList() {
		
		String sql = "select id,name,descript from category where pid is null";
		List<Category> list = null;
		Category category = null;
		try {
			conn = getConnection();
			psta = conn.prepareStatement(sql);
			rs = psta.executeQuery();
			while(rs.next()){
				if(list == null){
					list = new ArrayList<>();
				}
				category = new Category();
				category.setId(rs.getInt(1));
				category.setName(rs.getString(2));
				category.setDescription(rs.getString(3));
				list.add(category);
			}
			close(conn, psta, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}

	@Override
	public List<Category> findSecondListByPid(int pid) {
		String sql = "select id,name,descript from category where pid=?";
		List<Category> list = null;
		Category category = null;
		Category pcat = new Category();
		pcat.setId(pid);
		try {
			conn = getConnection();
			psta = conn.prepareStatement(sql);
			psta.setInt(1, pid);
			rs = psta.executeQuery();
			while(rs.next()){
				if(list == null){
					list = new ArrayList<>();
				}
				category = new Category();
				category.setId(rs.getInt(1));
				category.setName(rs.getString(2));
				category.setDescription(rs.getString(3));
				category.setPcategory(pcat);
				list.add(category);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}
